package com.hzncc.scaffold.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Date工具类
 */
public class ThreadLocalDateUtil {

  private static final String date_format = TimeUtils.sFStr;
  private static final String day_format = TimeUtils.dayFStr;
  private static final ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<>();
  private static final ThreadLocal<SimpleDateFormat> dayFormatThreadLocal = new ThreadLocal<>();

  public static SimpleDateFormat getDateFormat() {
    SimpleDateFormat df = dateFormatThreadLocal.get();
    if (df == null) {
      df = new SimpleDateFormat(date_format);
      dateFormatThreadLocal.set(df);
    }
    return df;
  }

  public static SimpleDateFormat getDayFormat() {
    SimpleDateFormat df = dayFormatThreadLocal.get();
    if (df == null) {
      df = new SimpleDateFormat(day_format);
      dateFormatThreadLocal.set(df);
    }
    return df;
  }

  public static String formatDate(Date date) {
    return getDateFormat().format(date);
  }

  public static String formatDay(Date date) {
    return getDayFormat().format(date);
  }

  public static Date parseDate(String strDate) throws ParseException {
    return getDateFormat().parse(strDate);
  }

  public static Date parseDay(String strDate) throws ParseException {
    return getDayFormat().parse(strDate);
  }


}